package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.pojo.AddressBook;
import com.itheima.pojo.R;
import com.itheima.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    AddressBookService addressBookService;

    /**
     * 新增地址
     *
     * @return
     */
    @PostMapping
    public R addAddress(@RequestBody AddressBook addressBook, HttpServletRequest req) {
        //获取当前登陆人的id
        long id = (long) req.getSession().getAttribute("one");

        //将登陆人的id存入
        addressBook.setUserId(id);

        addressBookService.save(addressBook);

        return R.success("添加成功");

    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R setDefault(@RequestBody AddressBook addressBook){
        //查询与userid 相同的地址
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        List<AddressBook> list = addressBookService.list(lambdaQueryWrapper);
        for (AddressBook book : list) {
            book.setIsDefault(0);
            addressBookService.updateById(book);
        }

        //将传过来的数据中默认改成一
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);

        return R.success(addressBook);
    }


    /**
     * 查询默认地址
     *
     */
    @GetMapping("/default")
    public R<AddressBook> queryDefault(){
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook one = addressBookService.getOne(lambdaQueryWrapper);
        if (one!=null){
            return R.success(one);
        }
        return R.error("m没有默认地址");

    }

    /**
     * 查询地址列表
     * @return
     */
    @GetMapping("/list")
    public R list(){
       LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
       lambdaQueryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        List<AddressBook> list = addressBookService.list(lambdaQueryWrapper);
        return R.success(list);
    }
    @DeleteMapping
    public R deleteByIds(long [] ids){
        for (long id : ids) {
            addressBookService.removeById(id);
        }
        return R.success("删除成功");
    }
    /**
     * 根据id回显数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R query(@PathVariable long id){
        AddressBook byId = addressBookService.getById(id);
        if (byId!=null){
            return R.success(byId);
        }
        return R.error("没有地址信息");
    }

    /**
     * 修改地址
     */
    @PutMapping
    public R update(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);

        return R.success("修改成功");
    }

}
